<template>
  <div class="register-container">
    <div class="register-content">
      <h2>用户注册</h2>
      <el-form :model="registerForm" :rules="registerRules" ref="registerFormRef" label-width="0">
        <el-form-item prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入账号" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleRegister">注册</el-button>
        </el-form-item>
      </el-form>
      <div class="login-link">
        已有账号？<router-link to="/">立即登录</router-link>
      </div>
    </div>
  </div>
</template>

<script>
import {RechargeMoney} from "@/service/User";

export default {
  data() {
    return {
      registerForm: {
        username: '',
        password: '',
      },
      registerRules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 20, message: '账号长度在 3 到 20 个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于 6 位', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    handleRegister() {
      RechargeMoney(this.registerForm).then(res => {
        if (res===1) {
          // 这里可以添加注册逻辑，例如发送请求到后端进行注册
          console.log('注册成功', this.registerForm);
          // 注册成功后跳转到登录页面
          this.$router.push({ path: '/' });
        } else if (res===2) {
          //弹窗
          this.$message.error('注册失败');
          return false;
        }
        else  if (res===3){
          this.$message.error('该用户已存在');
          this.$router.push({ path: '/' });
          return false;
        }
      })
    }
  }
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('~@/assets/0862a6cda3852a5.jpg');
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
}

.register-content {
  width: 400px;
  padding: 40px;
  color: white;
  background-color: rgba(0, 0, 0, 0.4);
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
}

.register-content h2 {
  font-size: 24px;
  margin-bottom: 20px;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input__inner {
  height: 40px;
  background-color: rgba(255, 255, 255, 0.1);
  border-color: transparent;
  color: white;
}

.el-input__inner::placeholder {
  color: rgba(255, 255, 255, 0.6);
}

.el-button {
  width: 100%;
  height: 40px;
  font-size: 16px;
}

.login-link {
  text-align: center;
  margin-top: 15px;
  color: rgba(255, 255, 255, 0.6);
}

.login-link a {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: underline;
}
</style>
